#!/usr/bin/env python
# -*- coding:utf-8 -*-
# @FileName  :识别文件后缀.py
# @Time      :2023/8/1 
# @Author    :CL
# @email     :1037654919@qq.com

# 1.数据清洗
# 2.文件格式识别数据后缀添加
# 3.
# 通过文件头判断文件格式
# 如果有后缀直接识别为后缀，主要针对无后缀文件
# 实现识别文件后缀
import os
import shutil
import json

def get_file_type(file_path):
    with open(file_path, 'rb') as f:
        file_head = f.read(4)
        if file_head[:3] == b'\xff\xd8\xff':
            return 'JPEG'
        elif file_head[:4] == b'\x89PNG':
            return 'PNG'
        elif file_head[:3] == b'ID3':
            return 'MP3'
        elif file_head[:4] == b'%PDF':
            return 'PDF'
        else:
            return 'unknown'

def file_type():
    file_path = 'huaban/test/47030424/'
    names = os.listdir(file_path)

    for name in names:
        path = file_path + name
        #
        # houzhui = os.path.splitext(path)[-1].lower()
        # if houzhui:
        #     print(houzhui)
        # else:
        #     file_type = get_file_type(path)
        #     print(file_type)
        #     # os.rename(path, path+'.'+file_type)
def count_file():
    # # 1.数据清洗
# # 2.文件格式识别数据后缀添加
# # 3.
    # file_type()
    base ='huaban/'
    names1 = os.listdir(base)
    i = 0
    # names1 = ['test']
    for name1 in names1:
        path1 = base+name1
        print(path1)
        names2 =  os.listdir(path1)
        print(len(names2))
        i += len(names2)
    print(i)

def main():
    # # 1.数据清洗
# # 2.文件格式识别数据后缀添加
# # 3.
    # file_type()
    base  ='huaban/'
    names1 = os.listdir(base)
    i = 0
    # names1 = ['test']
    for name1 in names1:
        path1 = base+name1
        print(path1)
        names2 =  os.listdir(path1)
        print(names2)
        for name2 in names2:
            file_path =path1 + '/'+name2
            names = os.listdir(file_path)
            print(name2,len(names))

            # # 没有summary.json文件，删除该文件夹
            # if 'summary.json' not in names :
            #     shutil.rmtree(file_path)
            #     # print('summary.json')
            #     print(file_path,len(names))
            #     i += len(names)

            for name in names:
                path =file_path + '/'+name
                # print(path)

                # # 获取文件后缀
                # file_type = os.path.splitext(path)[-1].lower()
                # if file_type:
                #     # print(path)
                #     pass
                # else:
                #     file_type = get_file_type(path)
                #     print(file_type)
                #     os.rename(path, path+'.'+file_type)

                # # 删除unknown
                # if 'unknown' in name:
                #     print(path)
                #     os.remove(path)
            # 修改summary.json
            sum_path = file_path + '/' + 'summary.json'
            with open(sum_path, 'r') as fcc_file:
                fcc_data = fcc_file.read()
                # print((fcc_data))
                for name in names:
                    if 'txt' in name:
                        continue
                    data  = name.split('.')[0] + '\"'
                    if data in fcc_data:
                        fcc_data = fcc_data.replace(data, name+'"')
                # print((fcc_data))
            fcc_file.close()
            with open(sum_path, 'w') as fcc_file:
                # fcc_data = json.loads(fcc_data)
                # fcc_file.write(json.dumps(fcc_data, ensure_ascii=False))

                fcc_file.write(fcc_data)
            fcc_file.close()


if __name__ =="__main__":
    count_file()